const Fs = require('fs');
const Path = require('path');
const { ipcMain, BrowserWindow } = require('electron');
const Msg = require('./msg');
let MainWin = null, PageWin = null;
module.exports = {
    initMain () {
        ipcMain.on(Msg.CreateWin, (event, data) => {
            this.createPageWindow();
            event.returnValue = null;
        });
        ipcMain.on(Msg.GetPageValue, async (event, data) => {
            event.returnValue = await this.getPageValue();
        });
    },
    async getPageValue () {
        return new Promise((resolve, reject) => {
            MainWin.webContents.once('ipc-message', (event, data) => {
                resolve(data);
            });
            MainWin.webContents.send(Msg.GetPageValue);
        });
    },
    _createWin (file) {
        let win = new BrowserWindow({
            webPreferences: {
                webviewTag: true,
                webSecurity: true,
                nodeIntegration: true, // 允许在渲染进程使用node特性
            },
        });
        let html = Path.join(__dirname, file);
        win.loadURL(`file://${html}`);
        win.openDevTools();
        return win;
    },
    createMainWindow () {
        MainWin = this._createWin('page/main.html');
    },
    createPageWindow () {
        PageWin = this._createWin('page/page.html');
    }
};